package com.example.aslongastoday.ui.mine;

import static android.content.Context.MODE_PRIVATE;

import androidx.core.view.GravityCompat;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.ViewModelProvider;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.example.aslongastoday.MainActivity;
import com.example.aslongastoday.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MineFragment extends Fragment implements AdapterView.OnItemClickListener{

    private View view;
    private ListView listview;
    private SimpleAdapter simpleAdapter;
    private ImageButton button;
    private LoginFragment Fragment;
    private VersionFragment Version_Fragment;
    private Bundle bundle;
    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;
    private TextView tv_userName;
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        view= inflater.inflate(R.layout.fragment_mine,container, false);
        listview = view.findViewById(R.id.mine_List);
        button = view.findViewById(R.id.mine_head);

        SharedPreferences sharedPreferences = getActivity().getSharedPreferences("loginInfo", MODE_PRIVATE);
        String username = sharedPreferences.getString("loginUserName", "--");
        tv_userName = view.findViewById(R.id.tv_userName);
        tv_userName.setText(username);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Toast.makeText( getContext(),"我是头像我被点了!", Toast.LENGTH_SHORT).show();


                Fragment = new LoginFragment();
                bundle = new Bundle();
                fragmentManager = getActivity().getSupportFragmentManager();
                fragmentTransaction = fragmentManager.beginTransaction();
                //跳转功能
                fragmentTransaction.add(R.id.container,Fragment);
                fragmentTransaction.setPrimaryNavigationFragment(Fragment);
                //可返回上一fragment
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();

            }
        });
        simpleAdapter = new SimpleAdapter(getActivity(),getData(),R.layout.moudle_mine_classic,new String[]{"title","image"},new int[]{R.id.mine_text,R.id.mine_image});
        listview .setAdapter(simpleAdapter);
        listview.setOnItemClickListener(this);

        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }


    private List<Map<String,Object>> getData(){
        String [] titles ={"设置中心",
                "关于我们",
                "版本信息",
                "退出登录"};
        int [] images ={R.drawable.ic_launcher,
                R.drawable.ic_launcher,
                R.drawable.ic_launcher,
                R.drawable.ic_launcher};
        List<Map<String,Object>> list= new ArrayList<>();
        //List中存放Map,Map存放标题和图片的id
        for(int i=0;i<4;i++){
            Map  map = new HashMap();
            map.put("title",titles[i]);
            map.put("image",images[i]);
            list.add(map);
        }
        return list;
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        System.out.println(i);
        switch (i) {
            case 0:
                break;
            case 1:
                break;
            case 2:
                Version_Fragment = new VersionFragment();
                bundle = new Bundle();
                fragmentManager = getActivity().getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                //跳转功能
                fragmentTransaction.add(R.id.container, Version_Fragment);
                //返回可到原处
                fragmentTransaction.setPrimaryNavigationFragment(Version_Fragment);
                //可返回上一fragment
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
                break;
            case 3:
                SharedPreferences sharedPreferences = getActivity().getSharedPreferences("loginInfo", MODE_PRIVATE);
                String username = sharedPreferences.getString("loginUserName", null);
                if (username == null) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                    builder.setMessage("当前无用户")
                            .setPositiveButton("确定", null);
                    AlertDialog dialog = builder.create();
                    dialog.show();
                } else {
                    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                    builder.setMessage("确定要退出登录吗？")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    SharedPreferences sp = getContext().getSharedPreferences("loginInfo", MODE_PRIVATE);
                                    //获取编辑器
                                    SharedPreferences.Editor editor = sp.edit();
                                    editor.remove("loginUserName");
                                    editor.apply();
                                    Toast.makeText(getContext(), "退出成功", Toast.LENGTH_SHORT).show();
                                    Intent intent = new Intent(getActivity(), MainActivity.class);
                                    startActivity(intent);
                                    getActivity().finish();
                                }
                            })
                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                }
                            });
                    AlertDialog dialog = builder.create();
                    dialog.show();
                    break;
                }
            default:
                break;
        }
    }
}